package com.app.miwawa.api.eam.service.bo;

import java.util.List;

import javax.annotation.Resource;

import org.framework.common.api.entity.CommonEntityAware;
import org.framework.common.api.service.bo.CommonBoAdapter;
import org.framework.common.api.service.bo.CommonBoAware;
import org.framework.common.api.support.model.ProcessModel;
import org.framework.common.ext.util.StringUtil;
import org.framework.common.ext.util.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.app.miwawa.api.eam.entity.Classes;
import com.app.miwawa.api.eam.entity.ClassesRelation;
import com.app.miwawa.api.eam.entity.ClassesTime;
import com.app.miwawa.api.eam.entity.view.ClassesTeacher;
import com.app.miwawa.api.eam.service.dao.ClassesDao;
import com.app.miwawa.api.esm.entity.customer.Customer;

@Service("classesBo")
public class ClassesBo extends CommonBoAdapter<Classes, String> {

	
	@Autowired
	private ClassesDao classesDao;
	
	@Resource(name = "defaultBo")
	private CommonBoAware<CommonEntityAware<String>, String> defaultBo;
	
	/**
	 * 新增
	 * @see org.framework.common.api.service.bo.CommonBoAdapter#insert(org.framework.common.api.entity.CommonEntityAware)
	 * @author chanlong(陈龙)
	 * @date 2016年1月23日 上午9:44:41
	 */
	@Override
	public Classes insert(final Classes entity) throws Exception {
		return classesDao.save(entity);
	}

	/**
	 * 新增关联
	 * @see org.framework.common.api.service.bo.CommonBoAdapter#insertRelation(org.framework.common.api.entity.CommonEntityAware)
	 * @author chanlong(陈龙)
	 * @date 2016年2月3日 下午12:37:37
	 */
	@Override
	public Classes insertRelation(final Classes entity) throws Exception {
		List<ClassesTime> times = entity.getTimes();
		for (ClassesTime time : times) {
			time.setClassesId(entity.getId());
			defaultBo.insert(time);
		}
		return entity;
	}
	
	/**
	 * 新增关联
	 * @see org.framework.common.api.service.bo.CommonBoAdapter#insertRelations(org.framework.common.api.entity.CommonEntityAware)
	 * @author chanlong(陈龙)
	 * @date 2016年2月3日 下午12:20:17
	 */
	@Override
	public Classes insertRelations(final Classes entity) throws Exception {
		List<ClassesRelation> relations = entity.getRelations();
		for (ClassesRelation relation : relations) {
			relation.setClassesid(entity.getId());
			defaultBo.insert(relation);
		}
		return entity;
	}
	
	/**
	 * 更新班级信息
	 * @see org.framework.common.api.service.bo.CommonBoAdapter#update(org.framework.common.api.entity.CommonEntityAware)
	 * @author chanlong(陈龙)
	 * @date 2016年4月9日 下午5:58:01
	 */
	@Override
	public Classes update(final Classes entity) throws Exception {
		List<ClassesTime> times = entity.getTimes();
		List<ClassesRelation> relations = entity.getRelations();
		
		// 更新班级信息
		Classes classes = (Classes)defaultBo.update(entity);
		
		// 更新班级上课时间
		for (ClassesTime time : times) {
			defaultBo.update(time);
		}
		
		// 更新班级关联表（班级课程关联、班级教室关联、班级老师关联）
		for (ClassesRelation relation : relations) {
			String target = relation.getTarget();
			String targetid = relation.getTargetid();
			String classesid = relation.getClassesid();
			
			if(StringUtil.isNotEmpty(targetid) && StringUtil.isNotEmpty(classesid)){
				defaultBo.addParameter("target",target).addParameter("classesid",classesid).update(relation);
			}
		}
		return classes;
	}
			
	/**
	 * 删除老师和班级及课时费关系.
	 * @param entity
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年4月27日 下午4:50:27
	 */
	public void deleteRelation(final ClassesTeacher entity) throws Exception {
		ProcessModel process = ProcessModel.factory();
		// 删除老师和班级的关系
		process.jdbc.update(process.properties.getProperty("classes.delete.teacher"), entity.getTeacherid(), entity.getClassesid());
		
		// 删除老师课时费的关系
		process.jdbc.update(process.properties.getProperty("classes.delete.charge"), entity.getChargeid());
	}
	
	/**
	 * 查询最大编码.
	 * @return
	 * @author chanlong(陈龙)
	 * @date 2016年1月23日 下午5:36:44
	 */
	public String maxcode(){
		String code = classesDao.maxcode();
		return SystemUtils.maxcode(Classes.CODE_PREFIX_CLASSES, code, Customer.DEFAULT_CODE_SERIAL);
	}
}
